// jwx.js
let jweixin = require('jweixin-module')

function configWeiXin(callback) {
	// wxConfig(encodeURIComponent(location.href.split('#')[0]),callback);
	wxConfig(location.href.split('#')[0], callback);


}
function wxConfig(surl, callback) {
	uni.request({
		// url: 'https://mallmgt.inja.com/api/user/uLoginWithMobile',
 url: 'https://mallmgt.inja.com/api/user/getSignature',  
	  method: 'POST',
		header: {
			'content-type': 'application/x-www-form-urlencoded'  //post一定要带这个header，被这里坑了半天  
		},
		data: {
			url: surl
		},
		success: res => {
			let resConfig = res.data.data
			if (resConfig) {
				let apiList = [ // 可能需要用到的能力
					'onMenuShareAppMessage',
					'onMenuShareTimeline',
					'hideOptionMenu',
					'showOptionMenu',
					'chooseWXPay',
					'scanQRCode'
				];

				let info = {
					debug: false, // 调试，发布的时候改为false
					// appId: 'wxdfb771523cd8e076',
					appId: resConfig.appId,
					nonceStr: resConfig.nonceStr,
					timestamp: resConfig.timestamp,
					signature: resConfig.signature,
					jsApiList: apiList
				};
				jweixin.config(info);
				jweixin.error(err => {
					console.log('config fail:', err);
					// alert('config fail:', err)
				});

				jweixin.ready(res => {
					if (callback) callback(jweixin); // 配置成功
					// alert('config success:', res)
				});
			}

		},
		fail: () => {
			console.log('request fail', err);
		},
		complete: () => { }
	});
}
module.exports = {
	configWeiXin: configWeiXin

}